﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using ThuPhap.Class;
using System.ComponentModel;
using ThuPhap.Models;
using System.IO.IsolatedStorage;
using System.IO;

namespace ThuPhap
{
    public partial class PreviewPage : PhoneApplicationPage
    {
        public static WriteableBitmap writeableBitmap;
        public PreviewPage()
        {
            InitializeComponent();
            this.ImgZoom.Source = writeableBitmap;
        }

        private void ApplicationBar_StateChanged(object sender, Microsoft.Phone.Shell.ApplicationBarStateChangedEventArgs e)
        {
            this.ApplicationBar.Opacity = this.ApplicationBar.Opacity == 1 ? 0 : 1;
        }

        private void ExportApplicationBarMenuItem_Click(object sender, EventArgs e)
        {
            //ExportImage export = new ExportImage();
            //try
            //{
            //    string filenameIsolatedStorage = export.ExportUsingWriteableBitmap(writeableBitmap);
            //    string filenameGalleryPhone = export.SaveToSavedImagePhone(filenameIsolatedStorage);
            //    MessageBox.Show("Exported image successfully!", "Notice", MessageBoxButton.OK);
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK);
            //}
            string captain = "Notice";
            string message = "Do you want to export image?";
            string question = "Save exported image to your pictures";
            string type = "preview";
            NavigationService.Navigate(new Uri(string.Format("/MessageBoxControl.xaml?type={3}&captain={0}&message={1}&question={1}",captain,message,question,type), UriKind.Relative));
            MessageBoxControl.OKEventPreview += new MessageBoxControl.OKDelegate(MessageBoxControl_OKEventPreview);
            #region old region
            /*
            var result = MessageBox.Show("Do you want to export image?", "Notice", MessageBoxButton.OKCancel);

            if (result == MessageBoxResult.OK)
            {
                try
                {
                    //this.UnSelectedChildren();

                    BackgroundWorker exportBackgroundWorker = new BackgroundWorker();
                    App.LoadingPopup.IsOpen = true;
                    ExportImage export = new ExportImage();
                    //try
                    //{
                    //    string filenameIsolatedStorage = export.ExportUsingWriteableBitmap(MainCanvas);
                    //    string filenameGalleryPhone = export.SaveToSavedImagePhone(filenameIsolatedStorage);
                    //    MessageBox.Show("Exported image successfully!", "Notice", MessageBoxButton.OK);
                    //}
                    //catch (Exception ex)
                    //{
                    //    MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK);
                    //}

                    exportBackgroundWorker.DoWork += ((s, ev) =>
                    {

                        ev.Result = export.ExportUsingWriteableBitmap(writeableBitmap);

                    });


                    exportBackgroundWorker.RunWorkerCompleted += ((s, ev) =>
                    {
                        string filename = ev.Result as string;

                        //string filename = export.SaveAsJPGToIsolated();
                        //export.SaveToSavedImagePhone(filename);
                        SaveGalleryData saveGallery = new SaveGalleryData();
                        saveGallery.AddGalleryItem(new Models.GalleryItem() { Name = filename, Date = DateTime.Now, Image = filename });
                        App.LoadingPopup.IsOpen = false;
                        MessageBox.Show("Exported image successfully!", "Notice", MessageBoxButton.OK);
                        //NavigationService.Navigate(new Uri("/MessageBox.xaml", UriKind.Relative));
                        
                        //    //if (result == "1")
                        //    //{
                        //    //    MessageBox.Show("Exported image successfully!", "Notice", MessageBoxButton.OK);
                        //    //}
                        //    //else
                        //    //{
                        //    //    MessageBox.Show(result, "Error", MessageBoxButton.OK);
                        //    //}

                    });

                    exportBackgroundWorker.RunWorkerAsync();
                }
                catch (Exception ex)
                {
                    App.LoadingPopup.IsOpen = false;
                    MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK);
                }
                finally
                {
                    //App.LoadingPopup.IsOpen = false;
                }
            }*/
            #endregion
        }

        void MessageBoxControl_OKEventPreview(bool IsGallery)
        {
            try
            {
                //this.UnSelectedChildren();

                BackgroundWorker exportBackgroundWorker = new BackgroundWorker();
                App.LoadingPopup.IsOpen = true;
                ExportImage export = new ExportImage();
                //try
                //{
                //    string filenameIsolatedStorage = export.ExportUsingWriteableBitmap(MainCanvas);
                //    string filenameGalleryPhone = export.SaveToSavedImagePhone(filenameIsolatedStorage);
                //    MessageBox.Show("Exported image successfully!", "Notice", MessageBoxButton.OK);
                //}
                //catch (Exception ex)
                //{
                //    MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK);
                //}

                exportBackgroundWorker.DoWork += ((s, ev) =>
                {

                    ev.Result = export.ExportUsingWriteableBitmap(writeableBitmap);

                });


                exportBackgroundWorker.RunWorkerCompleted += ((s, ev) =>
                {
                    string filename = ev.Result as string;

                    //string filename = export.SaveAsJPGToIsolated();
                    //export.SaveToSavedImagePhone(filename);
                    SaveGalleryData saveGallery = new SaveGalleryData();
                    var gallery = new Models.GalleryItem() { Name = filename, Date = DateTime.Now, Image = filename };
                    saveGallery.AddGalleryItem(gallery);
                    App.LoadingPopup.IsOpen = false;
                    if (IsGallery)
                    {
                        export.SaveToSavedImagePhone(filename);
                    }
                    Deployment.Current.Dispatcher.BeginInvoke(new AddItemToGalleryDelegate(AddItemToGallery), gallery);
                    MessageBox.Show("Exported image successfully!", "Notice", MessageBoxButton.OK);
                    //NavigationService.Navigate(new Uri("/MessageBox.xaml", UriKind.Relative));

                    //    //if (result == "1")
                    //    //{
                    //    //    MessageBox.Show("Exported image successfully!", "Notice", MessageBoxButton.OK);
                    //    //}
                    //    //else
                    //    //{
                    //    //    MessageBox.Show(result, "Error", MessageBoxButton.OK);
                    //    //}
                    MessageBoxControl.OKEventPreview -= new MessageBoxControl.OKDelegate(MessageBoxControl_OKEventPreview);
                });

                exportBackgroundWorker.RunWorkerAsync();
            }
            catch (Exception ex)
            {
                App.LoadingPopup.IsOpen = false;
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK);
            }
            finally
            {
                //App.LoadingPopup.IsOpen = false;
            }
        }

        private delegate void AddItemToGalleryDelegate(GalleryItem Item);
        private void AddItemToGallery(GalleryItem Item)
        {
            using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(Item.Image, FileMode.Open, FileAccess.Read))
                {
                    WriteableBitmap writeableBitmap = new WriteableBitmap(0, 0).FromStream(fileStream);
                    var result = writeableBitmap.Crop(0, (writeableBitmap.PixelHeight - writeableBitmap.PixelWidth) / 2, writeableBitmap.PixelWidth, writeableBitmap.PixelWidth);
                    fileStream.Close();
                    Item.Bitmap = result;
                }
            }
            App.StartPageViewModel.GalleryItems.Insert(0, Item);
        }

        // these two fields fully define the zoom state:
        private double TotalImageScale = 1d;
        private Point ImagePosition = new Point(0, 0);


        private const double MAX_IMAGE_ZOOM = 3;
        private Point _oldFinger1;
        private Point _oldFinger2;
        private double _oldScaleFactor;


        #region Event handlers

        /// <summary>
        /// Initializes the zooming operation
        /// </summary>
        private void OnPinchStarted(object sender, PinchStartedGestureEventArgs e)
        {
            _oldFinger1 = e.GetPosition(ImgZoom, 0);
            _oldFinger2 = e.GetPosition(ImgZoom, 1);
            _oldScaleFactor = 1;
        }

        /// <summary>
        /// Computes the scaling and translation to correctly zoom around your fingers.
        /// </summary>
        private void OnPinchDelta(object sender, PinchGestureEventArgs e)
        {
            var scaleFactor = e.DistanceRatio / _oldScaleFactor;
            if (!IsScaleValid(scaleFactor))
                return;

            var currentFinger1 = e.GetPosition(ImgZoom, 0);
            var currentFinger2 = e.GetPosition(ImgZoom, 1);

            var translationDelta = GetTranslationDelta(
                currentFinger1,
                currentFinger2,
                _oldFinger1,
                _oldFinger2,
                ImagePosition,
                scaleFactor);

            _oldFinger1 = currentFinger1;
            _oldFinger2 = currentFinger2;
            _oldScaleFactor = e.DistanceRatio;

            UpdateImageScale(scaleFactor);
            UpdateImagePosition(translationDelta);
        }

        /// <summary>
        /// Moves the image around following your finger.
        /// </summary>
        private void OnDragDelta(object sender, DragDeltaGestureEventArgs e)
        {
            var translationDelta = new Point(e.HorizontalChange, e.VerticalChange);

            if (IsDragValid(1, translationDelta))
                UpdateImagePosition(translationDelta);
        }

        /// <summary>
        /// Resets the image scaling and position
        /// </summary>
        private void OnDoubleTap(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
            ResetImagePosition();
        }

        #endregion

        #region Utils

        /// <summary>
        /// Computes the translation needed to keep the image centered between your fingers.
        /// </summary>
        private Point GetTranslationDelta(
            Point currentFinger1, Point currentFinger2,
            Point oldFinger1, Point oldFinger2,
            Point currentPosition, double scaleFactor)
        {
            var newPos1 = new Point(
             currentFinger1.X + (currentPosition.X - oldFinger1.X) * scaleFactor,
             currentFinger1.Y + (currentPosition.Y - oldFinger1.Y) * scaleFactor);

            var newPos2 = new Point(
             currentFinger2.X + (currentPosition.X - oldFinger2.X) * scaleFactor,
             currentFinger2.Y + (currentPosition.Y - oldFinger2.Y) * scaleFactor);

            var newPos = new Point(
                (newPos1.X + newPos2.X) / 2,
                (newPos1.Y + newPos2.Y) / 2);

            return new Point(
                newPos.X - currentPosition.X,
                newPos.Y - currentPosition.Y);
        }

        /// <summary>
        /// Updates the scaling factor by multiplying the delta.
        /// </summary>
        private void UpdateImageScale(double scaleFactor)
        {
            TotalImageScale *= scaleFactor;
            ApplyScale();
        }

        /// <summary>
        /// Applies the computed scale to the image control.
        /// </summary>
        private void ApplyScale()
        {
            ((CompositeTransform)ImgZoom.RenderTransform).ScaleX = TotalImageScale;
            ((CompositeTransform)ImgZoom.RenderTransform).ScaleY = TotalImageScale;
        }

        /// <summary>
        /// Updates the image position by applying the delta.
        /// Checks that the image does not leave empty space around its edges.
        /// </summary>
        private void UpdateImagePosition(Point delta)
        {
            var newPosition = new Point(ImagePosition.X + delta.X, ImagePosition.Y + delta.Y);

            if (newPosition.X > 0) newPosition.X = 0;
            if (newPosition.Y > 0) newPosition.Y = 0;

            if ((ImgZoom.ActualWidth * TotalImageScale) + newPosition.X < ImgZoom.ActualWidth)
                newPosition.X = ImgZoom.ActualWidth - (ImgZoom.ActualWidth * TotalImageScale);

            if ((ImgZoom.ActualHeight * TotalImageScale) + newPosition.Y < ImgZoom.ActualHeight)
                newPosition.Y = ImgZoom.ActualHeight - (ImgZoom.ActualHeight * TotalImageScale);

            ImagePosition = newPosition;

            ApplyPosition();
        }

        /// <summary>
        /// Applies the computed position to the image control.
        /// </summary>
        private void ApplyPosition()
        {
            ((CompositeTransform)ImgZoom.RenderTransform).TranslateX = ImagePosition.X;
            ((CompositeTransform)ImgZoom.RenderTransform).TranslateY = ImagePosition.Y;
        }

        /// <summary>
        /// Resets the zoom to its original scale and position
        /// </summary>
        private void ResetImagePosition()
        {
            TotalImageScale = 1;
            ImagePosition = new Point(0, 0);
            ApplyScale();
            ApplyPosition();
        }

        /// <summary>
        /// Checks that dragging by the given amount won't result in empty space around the image
        /// </summary>
        private bool IsDragValid(double scaleDelta, Point translateDelta)
        {
            if (ImagePosition.X + translateDelta.X > 0 || ImagePosition.Y + translateDelta.Y > 0)
                return false;

            if ((ImgZoom.ActualWidth * TotalImageScale * scaleDelta) + (ImagePosition.X + translateDelta.X) < ImgZoom.ActualWidth)
                return false;

            if ((ImgZoom.ActualHeight * TotalImageScale * scaleDelta) + (ImagePosition.Y + translateDelta.Y) < ImgZoom.ActualHeight)
                return false;

            return true;
        }

        /// <summary>
        /// Tells if the scaling is inside the desired range
        /// </summary>
        private bool IsScaleValid(double scaleDelta)
        {
            return (TotalImageScale * scaleDelta >= 1) && (TotalImageScale * scaleDelta <= MAX_IMAGE_ZOOM);
        }

        #endregion
    }
}